package com.fankai.lease.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fankai.lease.model.entity.ApartmentInfo;
import com.fankai.lease.model.entity.FacilityInfo;
import com.fankai.lease.model.entity.GraphInfo;
import com.fankai.lease.model.entity.LabelInfo;
import com.fankai.lease.model.enums.ItemType;
import com.fankai.lease.web.app.mapper.*;
import com.fankai.lease.web.app.service.ApartmentInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fankai.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.fankai.lease.web.app.vo.apartment.ApartmentItemVo;
import com.fankai.lease.web.app.vo.graph.GraphVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Resource
    private ApartmentInfoMapper apartmentInfoMapper;

    @Resource
    private GraphInfoMapper graphInfoMapper;

    @Resource
    private LabelInfoMapper labelInfoMapper;
    @Resource
    private FacilityInfoMapper facilityInfoMapper;

    @Resource
    private RoomInfoMapper roomInfoMapper;

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        if (apartmentInfo == null) {
            return null;
        }
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfoList = graphInfoMapper.selectList(graphInfoLambdaQueryWrapper);
        ArrayList<GraphVo> graphVoArrayList = new ArrayList<>();
        graphInfoList.forEach(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo, graphVo);
            graphVoArrayList.add(graphVo);
        });
        apartmentDetailVo.setGraphVoList(graphVoArrayList);
        List<LabelInfo> labelApartInfoList = labelInfoMapper.selectLabelInfoListByApartId(id);
        apartmentDetailVo.setLabelInfoList(labelApartInfoList);
        BigDecimal minRent = roomInfoMapper.selectMinRentByApartId(id);
        apartmentDetailVo.setMinRent(minRent);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoVoListByApartId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        return apartmentDetailVo;
    }
}




